package com.swanclouds.redfish.systems;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Preconditions;
import com.swanclouds.api.systems.BiosService;
import com.swanclouds.core.transport.ClientConstants;
import com.swanclouds.model.ModelEntity;
import com.swanclouds.model.common.ActionResponse;
import com.swanclouds.model.common.ErrorResponse;
import com.swanclouds.model.systems.BIOSAttribute;
import com.swanclouds.redfish.internal.BaseSystemService;
import com.swanclouds.redfish.systems.domain.Bios;
import com.swanclouds.redfish.systems.domain.FutureBIOSSettings;

/**
 *  
 */
public class BiosServiceImpl extends BaseSystemService implements BiosService {

    /**
     * 查询BIOS资源信息
     *
     * @return Bios
     */
    @Override
    public Bios get() {
        return get(Bios.class,uri(ClientConstants.SYSTEMS_BIOS,getSystemId())).execute();
    }

    /**
     * 查询 BIOS 设置资源信息
     * /redfish/v1/Systems/{system_id}/Bios/SD
     *
     * @return the
     */
    @Override
    public FutureBIOSSettings getSD() {
        return get(FutureBIOSSettings.class, uri(ClientConstants.SYSTEMS_BIOS_SD, getSystemId())).execute();
    }

    /**
     * 修改 BIOS 密码
     * BMC只是修改BIOS密码，BIOS密码生效需重启OS，且如果OS重启前有多次设置，以最后一次的设置请求为准。
     *
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     * @param roleName    [Supervisor: 修改BIOS管理员密码, UserPassword: 修改BIOS普通用户密码]
     * @return ActionResponse
     */
    @Override
    public ActionResponse changePassword(String oldPassword, String newPassword, String roleName) {
        Preconditions.checkNotNull(oldPassword, "OldPassword is null");
        Preconditions.checkNotNull(newPassword,"NewPassword is null");
        Preconditions.checkNotNull(newPassword,"PasswordName is null");
        return postWithResponse(uri(ClientConstants.SYSTEMS_BIOS_ChangePassword, getSystemId())).entity(new ChangePassword(oldPassword,newPassword,roleName)).execute();
    }

    /**
     * 修改 BIOS 资源信息
     *
     * @param attribute 属性key,value
     * @return ActionResponse
     */
    @Override
    public ActionResponse changeAttribute(BIOSAttribute attribute) {
        Preconditions.checkNotNull(attribute, "Attribute is null");
        return postWithResponse(uri(ClientConstants.SYSTEMS_BIOS_MODIFY_ATTRIBUTES, getSystemId())).entity(attribute).execute();
    }

    /**
     * 恢复 BIOS 属性默认值
     */
    @Override
    public ErrorResponse resetBios() {
        return post(ErrorResponse.class, uri(ClientConstants.SYSTEMS_BIOS_ACTIONS_RESET, getSystemId())).execute();
    }

    private class ChangePassword implements ModelEntity {

        @JsonProperty("OldPassword")
        private String oldPassword;

        @JsonProperty("NewPassword")
        private String newPassword;

        @JsonProperty("PasswordName")
        private String passwordName;

        public ChangePassword(String oldPassword, String newPassword, String passwordName) {
            this.oldPassword = oldPassword;
            this.newPassword = newPassword;
            this.passwordName = passwordName;
        }

        public String getOldPassword() {
            return oldPassword;
        }

        public void setOldPassword(String oldPassword) {
            this.oldPassword = oldPassword;
        }

        public String getNewPassword() {
            return newPassword;
        }

        public void setNewPassword(String newPassword) {
            this.newPassword = newPassword;
        }

        public String getPasswordName() {
            return passwordName;
        }

        public void setPasswordName(String passwordName) {
            this.passwordName = passwordName;
        }

        @Override
        public String toString() {
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                return objectMapper.writeValueAsString(this);
            } catch (JsonProcessingException e) {
            }
            return "ChangePassword {" +
                    "oldPassword='" + oldPassword + '\'' +
                    ", newPassword='" + newPassword + '\'' +
                    ", passwordName='" + passwordName + '\'' +
                    '}';
        }
    }
}
